from gevent import monkey
monkey.patch_all()
from 代理.core.store.mysql_store import MONGOPOOL
from 代理.core.verify.http_verify import check_proxy
from 代理.setting import MAX_SCORE,TEST_PROXIES

from gevent.pool import Pool
from queue import Queue


class PROXY_TEST():
    def __init__(self):
        self.mongopool=MONGOPOOL()
        self.queue=Queue()
        self.coroutine=Pool()
    def coroutine_back(self,temp): #死循环
        self.coroutine.apply_async(self.__check_proxy__,callback=self.coroutine_back)
    def run(self):
        proxies=self.mongopool.find()
        print(proxies)
        for proxy in proxies:
            #把代理ip放到队列中
            self.queue.put(proxy)
        for i in range(1,TEST_PROXIES+1):
            self.coroutine.apply_async(self.__check_proxy__,callback=self.coroutine_back)
        #让当前的线程等待队列完成
        self.queue.join()

    def __check_proxy__(self):
        #从队列中取出proxy
        proxy=self.queue.get()
        #检查一个代理ip的可用性
        proxy = check_proxy(proxy)
        if proxy.speed == -1:
            proxy.score -= 1
            if proxy.score == 0:
                self.mongopool.delete_one(proxy)
            else:
                self.mongopool.update_one(proxy)
        else:
            proxy.score == MAX_SCORE
            self.mongopool.update_one(proxy)
        #队列任务完成
        self.queue.task_done()
    @classmethod
    def start(cls):
        #创建本类对象
        proxy_tester=cls()
        proxy_tester.run()
if __name__ == '__main__':
    pp=PROXY_TEST()
    pp.run()
